package input;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import util.Loteria;
import util.PropertiesUtil;

public class Main {

	public static void main(String[] args) {

		Properties props = PropertiesUtil.loadProperties("loteria.properties");

		Main main = new Main();
		int option = chooseOption();
		switch (option) {
		case 1:
			main.getResult(props.getProperty("megasena_url"),
					props.getProperty("megasena_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("megasena_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("megasena_result_file"),
					Loteria.MEGASENA);
			break;
		case 2:
			main.getResult(props.getProperty("lotomania_url"),
					props.getProperty("lotomania_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("lotomania_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("lotomania_result_file"),
					Loteria.LOTOMANIA);
			break;
		case 3:
			main.getResult(props.getProperty("lotofacil_url"),
					props.getProperty("lotofacil_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("lotofacil_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("lotofacil_result_file"),
					Loteria.LOTOFACIL);
			break;
		case 4:
			main.getResult(props.getProperty("megasena_url"),
					props.getProperty("megasena_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("megasena_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("megasena_result_file"),
					Loteria.MEGASENA);
			main.getResult(props.getProperty("lotomania_url"),
					props.getProperty("lotomania_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("lotomania_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("lotomania_result_file"),
					Loteria.LOTOMANIA);
			main.getResult(props.getProperty("lotofacil_url"),
					props.getProperty("lotofacil_download_name"),
					props.getProperty("download_folder"),
					props.getProperty("lotofacil_htm_file"),
					props.getProperty("result_folder"),
					props.getProperty("lotofacil_result_file"),
					Loteria.LOTOFACIL);
			break;
		}
	}

	private static int chooseOption() {
		int option = 0;
		do {
			System.out.println("Qual loteria gostaria de obter o resultado?");
			System.out.println("1 - Megasena");
			System.out.println("2 - Lotomania");
			System.out.println("3 - Lotofacil");
			System.out.println("4 - Todos");
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					System.in));
			try {
				option = Integer.parseInt(reader.readLine());

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NumberFormatException e) {
				// TODO: handle exception
				System.out.println("Opcao invalida");
			} catch (NullPointerException e) {
				// TODO: handle exception

			}
		} while (option == 0);
		return option;

	}

	public void getResult(String url, String downloadName,
			String downloadFolder, String htmFile, String resultFolder,
			String resultFile, Loteria loteria) {
		DownloadFile df = new DownloadFile();

		try {
			df.downloadFile(url, downloadName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		UnZip unzip = new UnZip();
		try {
			unzip.unzip(new File(downloadFolder, downloadName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		HtmToCSV converter = new HtmToCSV();

		converter.convert(
				new File(downloadFolder, htmFile),
				new File(resultFolder, resultFile), loteria);
	}
}
